package com.pushwish.server;

import java.io.IOException;
import java.util.Properties;
import java.util.logging.Logger;

import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class MailHandlerServlet extends HttpServlet {
	
	private static final long serialVersionUID = -4787785775602346818L;
	private static final Logger log = Logger.getLogger(MailHandlerServlet.class.getName());

	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		Properties props = new Properties();
		Session session = Session.getDefaultInstance(props, null);
		try {
			MimeMessage message = new MimeMessage(session, req.getInputStream());
//			String from = message.getContentType();
			InternetAddress[] ifrom = (InternetAddress[])message.getFrom();
			String sfrom = ( ifrom.length == 0) ? "nobody" : ifrom[0].getAddress();
			
			String replay = sfrom + " send you a email.";
			new PushwishToGTalk("guyuchun1969@gmail.com").push(replay);
			new PushwishToGMail().push(replay);
		} catch (MessagingException e) {
			log.severe(e.getMessage());
		}
	}
}
